package com.aitou.mrylive.presentation.ui.main.index;

import com.aitou.mrylive.data.bean.BaseResponse;
import com.aitou.mrylive.data.bean.HotAnchorSummary;
import com.aitou.mrylive.data.bean.PageBean;
import com.aitou.mrylive.domain.AnchorManager;
import com.aitou.mrylive.presentation.ui.base.BaseObserver;
import com.aitou.mrylive.presentation.ui.base.BasePresenter;
import com.aitou.mrylive.presentation.ui.base.page.PageRecorder;
import com.aitou.mrylive.util.L;

import java.util.List;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Muyangmin
 * @since 1.0.0
 */
public class FollowedAnchorPresenter extends BasePresenter<FollowedAnchorInterface> {

    private AnchorManager anchorManager;
    private PageRecorder pageRecorder;

    public FollowedAnchorPresenter(FollowedAnchorInterface uiInterface) {
        super(uiInterface);
        anchorManager = new AnchorManager();
        pageRecorder = new PageRecorder();
    }

    public void loadFirstPage() {
        Subscription subscription = anchorManager.loadFollowedLives(pageRecorder.getFirstPage())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<HotAnchorSummary>>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<List<HotAnchorSummary>> response) {
                        pageRecorder.moveToFirstPage();
                        L.i("Presenter", response.toString());
                        getUiInterface().showData(response.getData());
                    }
                });
        addSubscription(subscription);
    }

    public void loadNextPage() {
        Subscription subscription = anchorManager.loadFollowedLives(pageRecorder.getNextPage())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<HotAnchorSummary>>>(getUiInterface
                        ()) {
                    @Override
                    public void onSuccess(BaseResponse<List<HotAnchorSummary>> response) {
                        pageRecorder.moveToNextPage();
                        if (response.getData() != null) {
                            getUiInterface().appendData(response.getData());
                        }
                    }
                });
        addSubscription(subscription);
    }
}
